﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BFlatCompile.Serializer
{
	internal static class NativeCodeLookup
	{
		// current max: 55
		private static readonly Dictionary<string, int> lookup = new Dictionary<string, int>()
		{
			{ "fast_list_add", 51 },

			{ "game_draw_rectangle", 47 },

			{ "game_image_ctor_copy", 3 },
			{ "game_image_ctor_file", 20 },
			{ "game_image_ctor_size", 4 },
			{ "game_image_fill", 24 },
			{ "game_image_get_width", 6 },
			{ "game_image_get_height", 7 },

			{ "game_screen_ctor", 5 },
			{ "game_screen_blit", 19 },

			{ "game_window_blit_screen", 25 },
			{ "game_window_close", 55 },
			{ "game_window_ctor", 22 },
			{ "game_window_pop_event", 21 },
			{ "game_window_start", 23 },

			{ "os_file_openread", 38 },
			{ "os_file_close", 39 },
			{ "os_file_readalltext", 40 },

			{ "system_console_print", 1 },
			{ "system_console_read", 2 },

			{ "system_exception_serializestacktrace", 50 },

			{ "system_math_sin", 8 },
			{ "system_math_cos", 9 },
			{ "system_math_tan", 10 },
			{ "system_math_arcsin", 11 },
			{ "system_math_arccos", 12 },
			{ "system_math_arctan", 13 },
			{ "system_math_arctan2", 18 },
			{ "system_math_sqrt", 14 },
			{ "system_math_pow", 15 },
			{ "system_math_log", 16 },
			{ "system_math_ln", 17 },
			{ "system_math_parseint", 43 },
			{ "system_math_tryparseint", 44 },
			{ "system_math_floor", 48 },
			{ "system_math_ceiling", 49 },

			{ "system_primitivemethods_listadd", 26 },
			{ "system_primitivemethods_listaddlist", 32 }, 
			{ "system_primitivemethods_listclear", 27 },
			{ "system_primitivemethods_listinsert", 33 },
			{ "system_primitivemethods_listjoin", 36 },
			{ "system_primitivemethods_listremove", 52 },
			{ "system_primitivemethods_listreverse", 34 },
			{ "system_primitivemethods_mapadd", 28 },
			{ "system_primitivemethods_mapclear", 37 },
			{ "system_primitivemethods_mapcontains", 31 },
			{ "system_primitivemethods_mapget", 35 },
			{ "system_primitivemethods_stringsplit", 41 },
			{ "system_primitivemethods_stringsplitwhitespace", 42 },
			{ "system_primitivemethods_stringtrim", 45 },
			{ "system_primitivemethods_stringtrimchars", 46 },

			{ "system_random_nextint", 29 },
			{ "system_random_nextfloat", 30 },

			{ "system_time_get", 53 },
			{ "system_time_format", 54 },
		};

		private static bool verified = false;

		public static int Get(string key)
		{
			if (!verified)
			{
				Verify();
			}

			return lookup[key]; // OK to crash here on key not found.
		}

		private static void Verify()
		{
			HashSet<int> ids = new HashSet<int>();
			foreach (int value in lookup.Values)
			{
				if (ids.Contains(value))
				{
					throw new Exception("Duplicate native code ID#");
				}
				ids.Add(value);
			}
		}
	}
}
